﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace BPMS.Utility.Encryption
{
    public enum EncryptType
    {
        /// <summary>
        /// 默认
        /// </summary>
        Default = 0,
        /// <summary>
        /// 十六位
        /// </summary>
        Bit16 = 1,
        /// <summary>
        /// 三十二位
        /// </summary>
        Bit32 = 2
    }
    public class MD5Encryptor
    {

        /// <summary>
        /// 加密字符串
        /// </summary>
        /// <param name="source"></param>
        /// <param name="enType"></param>
        /// <returns></returns>
        public static string Encrypt(string source, EncryptType enType)
        {
            if (string.IsNullOrWhiteSpace(source))
                return string.Empty;
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] buffer = Encoding.UTF8.GetBytes(source);
            byte[] hashBytes = md5.ComputeHash(buffer);
            StringBuilder sbResult = new StringBuilder();
            foreach (byte item in hashBytes)
            {
                switch (enType)
                {
                    case EncryptType.Bit16:
                        sbResult.Append(item.ToString("X"));
                        break;
                    case EncryptType.Bit32:
                        sbResult.Append(item.ToString("X2"));
                        break;
                    default:
                        sbResult.Append(item.ToString().Replace("-", ""));
                        break;
                }
            }
            return sbResult.ToString();
        }
    }
}
